import random
import time
import requests
import re

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'
}


def get_ip(base_url):
    ip_list = []
    response = requests.get(base_url, headers=headers)
    ips = re.findall('<td data-title="IP">(.*?)</td>', response.text)
    ports = re.findall('<td data-title="PORT">(.*?)</td>', response.text)
    for i in range(len(ips)):
        a_ip = ips[i] + ':' + ports[i]
        ip_list.append(a_ip)
    print(ip_list)
    time.sleep(random.randint(1, 3))
    return ip_list


def try_ip(ips):
    urllib = ['http://www.baidu.com', 'http://www.huya.com', 'https://www.qidian.com/', 'http:www.bilibili.com']
    url = random.choice(urllib)
    proxies = {
        'http:': 'http://' + ips,
        'https:': 'https://' + ips
    }
    try:
        response = requests.get(url, headers=headers, proxies=proxies, timeput=3)
        if response.status_code == 200:
            return ips
        else:
            print('请求 != 200', ips)
    except:
        print("出现错误，无用的IP", ips)


if __name__ == '__main__':
    for i in range(1, 20):
        url = f'https://www.kuaidaili.com/free/inha/{i}'
        print(f'-------------------第{i}页开始-------------', url)
        ip_list = get_ip(url)
        for item in map(try_ip, ip_list):
            if item == None:
                pass
            else:
                print('可用的IP' + item)
                with open('可用的IP.txt', 'a+', encoding='utf-8')as f:
                    f.write(item + '\n')